package com.fruitbasket.audioplatform;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import com.chaquo.python.Kwarg;
import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;


import java.util.ArrayList;
import java.util.List;

public class CalculateService extends Service{

    private Context context = ContextUtil.getContext();
    private String TAG = "xianjian";
    Python py = null;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG,"onBind");
        return null;
    }

    @Override
    public void onCreate() {
        Log.i(TAG,"onCreate");
        super.onCreate();
        Log.i(TAG,"onCreate preinitPython");
        initPython();
        Log.i(TAG,"onCreate postinitPython");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG,"onStartCommand");
        String assetsDir = getExternalFilesDir(null).toString();
        Log.i(TAG,assetsDir);
        //do_python(); //use thread to process the time killer
        new Thread(new Runnable() {
            @Override
            public void run() {
                do_python2();
                //when process stop ,kill it
            }
        }).start();

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        Log.i(TAG,"onDestroy");
        super.onDestroy();
    }


    void initPython(){
        if (! Python.isStarted()) {
            Python.start(new AndroidPlatform(this));
        }
        py = Python.getInstance();
    }

    public void do_python() {
        Log.d(TAG,"====================mail send button test===================");
        String inputfile = getExternalFilesDir(null)+ "/input2.jpg";        // file exsit or not
        SharedPreferences sharedPreferences= getSharedPreferences("userData", Context .MODE_PRIVATE);
//        String tmpfile_path=sharedPreferences.getString("tmpfile","input.jpg");
//        String inputfile = tmpfile_path;        // file exsit or not
        Log.i(TAG,"=======>>>>>>>>>======="+inputfile);
        String colorfile = getExternalFilesDir(null) + "/color.jpg";
        String resultfile = getExternalFilesDir(null) + "/result.jpg";
        String filePath  =  getExternalFilesDir(null) + "/";
        try {
            py.getModule("kmeans2").callAttr("rdis_wall", inputfile, colorfile, resultfile, filePath );
            Log.d(TAG,"====================python done===========================");
        }catch (com.chaquo.python.PyException e)
        {
            Log.d(TAG,"====================python exception===========================");
        }
    }

    public void do_python2() {
        Log.d(TAG,"====================mail send button test===================");
        String resultfile = getExternalFilesDir(null)+ "/input.jpg";        // file exsit or not
        SharedPreferences sharedPreferences= getSharedPreferences("userData", Context .MODE_PRIVATE);
        Log.i(TAG,"=======>>>>>>>>>======="+resultfile);
        String savefigfile = getExternalFilesDir(null) + "/savefig.jpg";
        //String resultfile = getExternalFilesDir(null) + "/result.jpg";
        String filePath  =  getExternalFilesDir(null) + "/";
        try {
            //#genRadiusImg('result2.jpg',20,'./test1.jpg')
            PyObject obj1 = py.getModule("liantongyu").callAttr("genRadiusImg", resultfile, 10, savefigfile );
            List sum = obj1.asList();
            Log.i(TAG, sum.toString() + "and sum1 is " + sum.get(1));
            Log.d(TAG,"====================python done===========================");
        }catch (com.chaquo.python.PyException e)
        {
            e.printStackTrace();
            Log.d(TAG,"====================python exception===========================");
        }
    }

}
